class SceneManger {
    //单例模式改造
    static singleObj;
    static createObj(stage) {//stage是形参
        if (!SceneManger.singleObj) {
            SceneManger.singleObj = new SceneManger(stage);//stage是实参
        }
        return SceneManger.singleObj;
    }
    constructor(stage) {//stage是形参
        this._stage = stage;
    }
    private _stage;
    private curScene;
    private _groupName;
    private _loading;
    private _nextScene;
    public loadScene(groupName, nextScene) {//加载场景的方法
        this._groupName = groupName;
        this._nextScene = nextScene;
        this._loading = LoadingUI.createObj();
        this._stage.addChild(this._loading);//加载进度条
        //添加资源组加载事件
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);//资源组加载成功时触发
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);//资源组加载失败时触发
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);//资源组加载进度（每个条目加载成功时都会触发）
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);//资源组里面单个条目加载失败时触发
        RES.loadGroup(groupName);//执行加载资源组
    }
    private onResourceLoadComplete(e) {
        if (e.groupName == this._groupName) {//这句话判断时必须的
            //移除监听事件，RES没有once事件
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            let _nextScene = new this._nextScene();//实例化要加载的场景
            this._stage.addChild(_nextScene);//把要加载的场景添加到舞台
            if (this.curScene) {//如果curScene存在，即不是第一个进来场景
                if (_nextScene.enterAni) {//如果加载进来的场景有自己的进入动画
                    _nextScene.enterAni(() => {
                        this._stage.removeChild(this.curScene);
                        this._stage.removeChild(this._loading);
                        this.curScene = _nextScene;
                    });
                } else {//如果加载进来的场景没有自己的进入动画，则执行默认动画
                    _nextScene.x = this._stage.stageWidth;
                    egret.Tween.get(_nextScene).to({ x: 0 }, 500).call(() => {
                        this._stage.removeChild(this.curScene);
                        this._stage.removeChild(this._loading);
                        this.curScene = _nextScene;
                    });
                }

            } else {//如果要加载进来的场景是第一个场景
                this._stage.removeChild(this._loading);
                this.curScene = _nextScene;//加载进来的场景更新为当前场景
            }
        }
    }
    private onResourceLoadError(event: RES.ResourceEvent) {
        console.warn("Group:" + event.groupName + " has failed to load");
        this.onResourceLoadComplete(event);
    }
    private onResourceProgress(event: RES.ResourceEvent) {
        if (event.groupName == this._groupName) {
            this._loading.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }
    private onItemLoadError(event: RES.ResourceEvent) {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }
}


//默认的LoadingUI，后期如果有自己的loading样式，可删除，但记得类名必须是LoadingUI，必须有单例模式调用结构
class LoadingUI extends egret.Sprite {
    static singleObj;
    static createObj() {
        if (!LoadingUI.singleObj) {
            LoadingUI.singleObj = new LoadingUI();
        }
        return LoadingUI.singleObj;
    }

    public constructor() {
        super();
        // this.createView();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }

    private textField: egret.TextField;
    private progress: egret.DisplayObjectContainer;
    private frontRect: egret.Shape;
    private ron;
    private imgt;
    private jx;
    private pen1;
    private img1;
    private texture;
    private createView(): void {
        
        let imgLoder = new egret.ImageLoader();
            imgLoder.addEventListener(egret.Event.COMPLETE,function(e){
                var bmd = e.currentTarget.data;//拿到图片数据对象biMapData
                var texture = new egret.Texture();
                texture.bitmapData = bmd;//通过bitmapData生成一个纹理
                var img = new egret.Bitmap(texture);//把纹理赋值给Bitmap生成一个真正的位图
                this.addChildAt(img)
                img.width=this.stage.stageWidth;
                img.height=this.stage.stageHeight;
            },this)
            imgLoder.load("resource/assets/bgjpg.png"); 
        //实例化加载的容器
        this.ron = new egret.DisplayObjectContainer();
        this.addChild(this.ron)
        this.ron.width = 600;
        this.ron.height = 40;
        this.ron.x = 10
        this.ron.y = 450
        
        let yuan1 = new egret.Sprite();
        yuan1.graphics.beginFill(0xfefbfb);
        yuan1.graphics.drawCircle(0,50,15);
        yuan1.graphics.endFill;
        yuan1.x = 75
        this.ron.addChild(yuan1);

        let yuan2 = new egret.Sprite();
        yuan2.graphics.beginFill(0xfefbfb);
        yuan2.graphics.drawCircle(0,50,15);
        yuan2.graphics.endFill;
        yuan2.x = 150
        this.ron.addChild(yuan2);
 
        let yuan3 = new egret.Sprite();
        yuan3.graphics.beginFill(0xfefbfb);
        yuan3.graphics.drawCircle(0,50,15);
        yuan3.graphics.endFill;
        yuan3.x = 225
        this.ron.addChild(yuan3);

        let yuan4 = new egret.Sprite();
        yuan4.graphics.beginFill(0xfefbfb);
        yuan4.graphics.drawCircle(0,50,15);
        yuan4.graphics.endFill;
        yuan4.x = 300
        this.ron.addChild(yuan4);

        let yuan5 = new egret.Sprite();
        yuan5.graphics.beginFill(0xfefbfb);
        yuan5.graphics.drawCircle(0,50,15);
        yuan5.graphics.endFill;
        yuan5.x = 375
        this.ron.addChild(yuan5);

        let yuan6 = new egret.Sprite();
        yuan6.graphics.beginFill(0xfefbfb);
        yuan6.graphics.drawCircle(0,50,15);
        yuan6.graphics.endFill;
        yuan6.x = 450
        this.ron.addChild(yuan6);
        let yuan7 = new egret.Sprite();
        yuan7.graphics.beginFill(0xfefbfb);
        yuan7.graphics.drawCircle(0,50,15);
        yuan7.graphics.endFill;
        yuan7.x = 525
        this.ron.addChild(yuan7);

        let yuan8 = new egret.Sprite();
        yuan8.graphics.beginFill(0xfefbfb);
        yuan8.graphics.drawCircle(0,50,15);
        yuan8.graphics.endFill;
        yuan8.x = 600
        this.ron.addChild(yuan8);

        this.textField = new egret.TextField();
        this.addChild(this.textField);
        this.textField.y = this.stage.stageHeight - this.textField.height - 580;
        this.textField.width = this.stage.stageWidth;
        this.textField.height = 100;
        this.textField.textAlign = "center";
        this.textField.size = 35;

        let imgLoder1 = new egret.ImageLoader();
            imgLoder1.addEventListener(egret.Event.COMPLETE,function(e){
                var bmd = e.currentTarget.data;//拿到图片数据对象biMapData
                let texture = new egret.Texture();
                texture.bitmapData = bmd;//通过bitmapData生成一个纹理
                this.img1 = new egret.Bitmap(texture);//把纹理赋值给Bitmap生成一个真正的位图
                this.ron.addChild(this.img1)
                this.img1.width=50;
                this.img1.height=50;
                this.img1.x = 0
                this.img1.y = this.img1.height*0.5;
            },this)
            imgLoder1.load("resource/assets/tox.png");

    }

    public setProgress(current: number, total: number): void {
        var percent = Math.floor((current / total) * 100) + "%";
        console.log(percent);
        var pen1 = Math.floor((current / total)*600);
        this.textField.text = `Loading...${current}/${total}`;
        this.textField.text = `Loading...${percent}`;
        if(this.img1){
            this.img1.x = pen1-this.img1.width*0.5;
        }
        
    }

}

